﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Net.Http;
using System.Windows.Forms;
using IiiToolkitMain.Common.Dto;

namespace IiiToolkitMain.Controls
{
    
    public partial class VideoItem : UserControl
    {
        readonly HttpClient _http = new HttpClient();

        public event VideoItemPlayHandle OnVideoItemSelectedEvent;
        private Image img2;
        public VideoItem(CameraItem cameraInfo)
        {
            CameraInfo = cameraInfo;
            InitializeComponent();
            using (var fs = File.OpenRead("images/2.png"))
            {
                img2 = Image.FromStream(fs);
            }
            this.DeviceName = CameraInfo.deviceName;
            this.PicUrl = CameraInfo.picUrl;
            this.CameraId = CameraInfo.cameraId;
            this.DeviceSerial = CameraInfo.deviceSerial;
        }

        public CameraItem CameraInfo { get;private set; }

        private string DeviceName
        {
            get { return this.lblDeviceName.Text; }
            set { this.lblDeviceName.Text = value; }
        }

        private string _picUrl;

        private string PicUrl
        {
            get { return _picUrl; }
            set
            {
                _picUrl = value;
                SetPic(_picUrl);
            }
        }

        private string _deviceSerial;
        private string DeviceSerial
        {
            get { return _deviceSerial; }
            set { _deviceSerial = value; }
        }
        private string CameraId { get; set; }

        private async void SetPic(string picUrl)
        {
            this.picVideoCover.BackgroundImage = Image.FromStream(await _http.GetStreamAsync(picUrl));//取网络图片
            this.picVideoCover.BackgroundImageLayout = ImageLayout.Stretch;
        }
        private void VideoItem_MouseHover(object sender, EventArgs e)
        {
            ControlPaint.DrawBorder(this.CreateGraphics(), this.ClientRectangle, Color.DeepSkyBlue, ButtonBorderStyle.Solid);
            this.picVideoCover.Image = img2;
            this.picVideoCover.SizeMode = PictureBoxSizeMode.CenterImage;

        }
        private void VideoItem_MouseLeave(object sender, EventArgs e)
        {
            ControlPaint.DrawBorder(this.CreateGraphics(), this.ClientRectangle, Color.Gainsboro, ButtonBorderStyle.Solid);
            this.picVideoCover.Image = null;
        }
        private void VideoItem_Paint(object sender, PaintEventArgs e)
        {
            ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, Color.Gainsboro, ButtonBorderStyle.Solid);
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if(OnVideoItemSelectedEvent!=null)
                OnVideoItemSelectedEvent(this,new VideoItemPlayEventArgs(this.CameraInfo));
        }
    }
}
